package cn.tang.tframe.storage.utils;

import cn.tang.tframe.common.utils.lang.StringUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tanghc
 */
public class FileContentTypeUtil {


    private final static Logger log = LoggerFactory.getLogger(FileContentTypeUtil.class);
    private static List docList = Lists.newArrayList();
    private static List imageList = Lists.newArrayList();
    private static List musicList = Lists.newArrayList();
    private static List mediaList = Lists.newArrayList();


    public static final String stream_contentType = "application/octet-stream";
    public static final String xls_contentType = "application/vnd.ms-excel";
    public static final String xlsx_contentType = "application/x-xls";
    public static final String doc_contentType = "application/msword";
    public static final String avi_contentType = "video/avi";
    public static final String pdf_contentType = "application/pdf";
    public static final String exe_contentType = "application/x-msdownload";
    public static final String apk_contentType = "application/vnd.android.package-archive";
    public static final String txt_contentType = "text/plain";
    public static final String png_contentType = "image/png;image/x-png";
    private static Map<String, String> fileNameFix = new LinkedHashMap<String, String>();

    static {
        fileNameFix.put("txt", txt_contentType);
        fileNameFix.put("apk", apk_contentType);
        fileNameFix.put("exe", exe_contentType);
        fileNameFix.put("pdf", pdf_contentType);
        fileNameFix.put("avi", avi_contentType);
        fileNameFix.put("doc", doc_contentType);
        fileNameFix.put("xls", xls_contentType);
        fileNameFix.put("xlsx", xlsx_contentType);
        fileNameFix.put("png", png_contentType);
    }

    //
    static {
        docList.add("doc");
        docList.add("docx");
        docList.add("xls");
        docList.add("xlsx");
        docList.add("pdf");
        docList.add("text");
        docList.add("zip");
        docList.add("war");
        docList.add("rar");
        docList.add("jar");
        docList.add("txt");
        docList.add("ppt");


        imageList.add("jpg");
        imageList.add("png");
        imageList.add("jpeg");
        imageList.add("bmp");
        imageList.add("gif");
        imageList.add("jpeg2000");
        imageList.add("tiff");
        imageList.add("psd");
        imageList.add("svg");
        imageList.add("swf");

        musicList.add("mp3");
        musicList.add("wma");
        musicList.add("aiff");
        musicList.add("au");
        musicList.add("midi");
        musicList.add("apa");
        musicList.add("wav");
        musicList.add("ape");


        mediaList.add("avi");
        mediaList.add("wma");
        mediaList.add("rmvb");
        mediaList.add("rm");
        mediaList.add("flash");
        mediaList.add("mp4");
        mediaList.add("mid");
        mediaList.add("3gp");
        mediaList.add("fla");
        mediaList.add("wmv");

    }


    public static String checkFileType(String url) {

        if (StringUtils.isBlank(url)) {
            return null;
        }

        try {
            String type = url.substring(url.lastIndexOf(".") + 1).toLowerCase();
            if (docList.contains(type)) {
                return FileTypeEnum.doc.name();
            } else if (imageList.contains(type)) {
                return FileTypeEnum.image.name();
            } else if (musicList.contains(type)) {
                return FileTypeEnum.music.name();
            } else if (mediaList.contains(type)) {
                return FileTypeEnum.media.name();
            } else {
                return FileTypeEnum.other.name();
            }
        } catch (Exception e) {
            log.error("文件类型有误", e);
            return null;
        }
    }


    public static String checkFileType(String url, String type) {

        try {
            if (docList.contains(type)) {
                return FileTypeEnum.doc.name();
            } else if (imageList.contains(type)) {
                return FileTypeEnum.image.name();
            } else if (musicList.contains(type)) {
                return FileTypeEnum.music.name();
            } else if (mediaList.contains(type)) {
                return FileTypeEnum.media.name();
            } else {
                return FileTypeEnum.other.name();
            }
        } catch (Exception e) {
            log.error("文件类型有误", e);
            return null;
        }
    }


    public static String getContentType(String fileName) {
        String fileFix = StringUtils.substringAfterLast(fileName, ".");
        String type = fileNameFix.get(StringUtils.lowerCase(fileFix));
        if (null != type) {
            return type;
        }
        return stream_contentType;
    }


}
